// ActionScript file
package com.fileio {
    import flash.events.*;
    import flash.net.FileReference;
    import flash.net.URLRequest;
    
    import mx.controls.Button;
    import mx.controls.ProgressBar;
    import mx.core.UIComponent;

    public class FileDownload extends UIComponent {
        // Hard-code the URL of file to download to user's computer.
        public var fr:FileReference;

        public function FileDownload() {
        }

        /**
         * Set references to the components, and add listeners for the OPEN, 
         * PROGRESS, and COMPLETE events.
         */




        /**
         * Begin downloading the file specified in the DOWNLOAD_URL constant.
         */
        public static function startDownload(video:String,pb:ProgressBar):void {
        	var fr:FileReference;
        	fr = new FileReference;
        	fr.addEventListener(Event.OPEN, openHandler);
            fr.addEventListener(ProgressEvent.PROGRESS, progressHandler);
            fr.addEventListener(Event.COMPLETE, completeHandler);
            var request:URLRequest = new URLRequest();
            request.url = video;
            fr.download(request);
        }

        /**
         * When the OPEN event has dispatched, change the progress bar's label 
         * and enable the "Cancel" button, which allows the user to abort the 
         * download operation.
         */
        public static function openHandler(event:Event, pb:ProgressBar):void {
            pb.label = "DOWNLOADING %3%%";
        }
        
        /**
         * While the file is downloading, update the progress bar's status and label.
         */
        public static function progressHandler(event:ProgressEvent, pb:ProgressBar):void {
            pb.setProgress(event.bytesLoaded, event.bytesTotal);
        }

        /**
         * Once the download has completed, change the progress bar's label and 
         * disable the "Cancel" button since the download is already completed.
         */
        public static function completeHandler(event:Event, pb:ProgressBar):void {
            pb.label = "DOWNLOAD COMPLETE";
            pb.setProgress(0, 100);
        }
    }
}